﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Linq;
using System.Web.DynamicData;

namespace ThuChi
{
    public partial class QuanLyTaiKhoan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int maQuyen;
            try
            {
                maQuyen = Utilities.GetMaQuyen((int)Session["MaTaiKhoan"]);

            }
            catch //(Exception ex)
            {
                maQuyen = 0;
            }

            if (maQuyen == 3)
            {
                Response.Redirect("~/NotPermitted.aspx");
            }
            else if (maQuyen == 0)
            {
                Session["RefUrl"] = "~/QuanLyTaiKhoan.aspx";
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                if (maQuyen == 1)
                {
                    CommandField cmd = (CommandField)grvTaiKhoan.Columns[0];
                    cmd.ShowDeleteButton = true;
                }
                else
                {
                    CommandField cmd = (CommandField)grvTaiKhoan.Columns[0];
                    cmd.ShowDeleteButton = false;
                }
            }

            if (!IsPostBack)
            {
                if (Session["cboAcountIndex"] == null)
                    Session["cboAcountIndex"] = 1;
                cboAcountFilter.SelectedIndex = (int)Session["cboAcountIndex"];
            }
        }

        protected void cboAcountFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["cboAcountIndex"] = cboAcountFilter.SelectedIndex;
            switch (cboAcountFilter.SelectedIndex)
            {
                case 0:
                    Session["Max"] = 3;
                    Session["Min"] = 1;
                    Session["Max1"] = true;
                    Session["Min1"] = false;
                    break;
                case 1:
                    Session["Max"] = 3;
                    Session["Min"] = 1;
                    Session["Max1"] = false;
                    Session["Min1"] = false;
                    break;
                case 2:
                    Session["Max"] = 3;
                    Session["Min"] = 1;
                    Session["Max1"] = true;
                    Session["Min1"] = true;
                    break;
                case 3:
                    Session["Max"] = 1;
                    Session["Min"] = 1;
                    Session["Max1"] = true;
                    Session["Min1"] = false;
                    break;
                case 4:
                    Session["Max"] = 2;
                    Session["Min"] = 2;
                    Session["Max1"] = true;
                    Session["Min1"] = false;
                    break;
                case 5:
                    Session["Max"] = 3;
                    Session["Min"] = 3;
                    Session["Max1"] = true;
                    Session["Min1"] = false;
                    break;
            }
        }

        protected void grvTaiKhoan_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "Trang " + (grvTaiKhoan.PageIndex + 1) + " của " + grvTaiKhoan.PageCount;
            }
        }

        protected void grvTaiKhoan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int maQuyen;
                try
                {
                    maQuyen = Utilities.GetMaQuyen((int)Session["MaTaiKhoan"]);

                }
                catch //(Exception ex)
                {
                    maQuyen = 0;
                }
                if (maQuyen == 1)
                {
                    DynamicField fieldQuyen = (DynamicField)grvTaiKhoan.Columns[3];
                    fieldQuyen.ReadOnly = false;
                }
                else if(maQuyen == 2)
                {
                    DynamicField fieldQuyen = (DynamicField)grvTaiKhoan.Columns[3];
                    fieldQuyen.ReadOnly = true;
                }
            }
        }

        protected void grvTaiKhoan_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                lblErrorMessage.Text = "Không thể xóa tài khoản: " + e.Values[0].ToString();
                e.ExceptionHandled = true;
            }
            else
            {
                lblErrorMessage.Text = "";
            }
        }

        protected void cboPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grvTaiKhoan.PageSize = Convert.ToInt16(cboPageSize.SelectedValue);
        }

        protected void LinqDataSourceTaiKhoan_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            cboPageSize_SelectedIndexChanged(sender, e);
        }

        protected void grvTaiKhoan_PreRender(object sender, EventArgs e)
        {
            grvTaiKhoan.DataBind();
        }
    }
}